package com.burgeon.backend.SHOP.controller;

import com.burgeon.backend.SHOP.entity.PickingCard;
import com.burgeon.backend.SHOP.entity.PickingCardItem;
import com.burgeon.backend.SHOP.service.AppUserService;
import com.burgeon.backend.entity.Fields;
import com.burgeon.backend.entity.User;
import com.burgeon.backend.mappers.FieldsMapper;
import com.burgeon.backend.stsutil.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("shop/vip")
public class AppUserController extends BaseController {

    @Autowired
    private AppUserService appUserService;
    @Autowired
    private FieldsMapper fieldsMapper;
    /**
     * 小程序注册&绑定emp
     *
     * @param session
     * @return
     */
    @RequestMapping("loginByModEmp")
    public ResponseResult<Void> loginByModEmp(String name,String empId, String gender, String birthday, String img,String openid,
                                              String nickname,HttpSession session) {
        System.err.println("sessionId="+session.getId());
        String token = session.getAttribute("token").toString();
        String phone = session.getAttribute("phone").toString();
        String appOpenid = session.getAttribute("app_openid").toString();

        User user = new User();
        user.setImg(img);
        user.setPhone(phone);
        user.setToken(token);
        user.setName(name);
        user.setGender(gender);
        user.setBirthday(birthday);
        user.setAppOpenid(appOpenid);
        user.setOpenid(openid);
        user.setNickname(nickname);

        user.setEmp(empId);
        appUserService.AppUserLoginByModEmp(user,session);

        return new ResponseResult<>(SUCCESS);
    }

    /**
     * 手机app扫码修改会员导购 店铺
     * @param id empId
     * @param session
     * @return
     */
    @RequestMapping("modEmpData")
    public ResponseResult<HashMap> modEmpData(String id,HttpSession session) {
        System.err.println("sessionId="+session.getId());
        String phone = session.getAttribute("phone").toString();
        String token = session.getAttribute("token").toString();
        System.err.println("绑定导购的id======="+id);
        HashMap data = appUserService.modEmpData(phone, id, token);
        System.err.println("返回的信息"+data);

        return new ResponseResult<HashMap>(SUCCESS,data);
    }



    @RequestMapping("pickingcard")
    public ResponseResult<List<PickingCard>> getPickingcard(HttpSession session) {
        String phone = session.getAttribute("phone").toString();
        String token = session.getAttribute("token").toString();
        List<PickingCard> data = appUserService.getPickingCard(phone,token);

        return new ResponseResult<List<PickingCard>>(SUCCESS, data);
    }

    @RequestMapping("pickingcarditem")
    public ResponseResult<List<PickingCardItem>> getPickingcardItem(String id,HttpSession session) {
        String token = session.getAttribute("token").toString();
        List<PickingCardItem> data = appUserService.getPickingCardItem(id,token);

        return new ResponseResult<List<PickingCardItem>>(SUCCESS, data);
    }


    @RequestMapping("exit")
    public ResponseResult<Void> Exit(HttpSession session) {
        session.removeAttribute("phone");
        session.removeAttribute("uid");

        return new ResponseResult<>(SUCCESS);
    }

    @RequestMapping("setphone")
    public ResponseResult<Void> setPhone(String phone, String openid, HttpSession session) {
        String token = session.getAttribute("token").toString();
        Fields fields = fieldsMapper.GetDataToken(token);
        if ("Y".equals(fields.getPrefix())) {
            phone = fields.getPrefixValue() + phone;
        }
        session.setAttribute("phone", phone);
        session.setAttribute("app_openid", openid);

        return new ResponseResult<>(SUCCESS);
    }

    @RequestMapping("getopenid")
    public ResponseResult<String> appGetOpenid(HttpSession session) {
        String data = session.getAttribute("app_openid").toString();

        return new ResponseResult<String>(SUCCESS, data);
    }

    /**
     * 小程序注册
     *
     * @param session
     * @return
     */
    @RequestMapping("login")
    public ResponseResult<Void> Login(String name, String gender, String birthday, String store,String emp, String img,String openid,
                                      String nickname,HttpSession session) {
        System.err.println("sessionId="+session.getId());
        String token = session.getAttribute("token").toString();
        String phone = session.getAttribute("phone").toString();
        String appOpenid = session.getAttribute("app_openid").toString();

        User user = new User();
        user.setImg(img);
        user.setPhone(phone);
        user.setToken(token);
        user.setName(name);
        user.setGender(gender);
        user.setBirthday(birthday);
        user.setAppOpenid(appOpenid);
        user.setOpenid(openid);
        user.setStore(store);
        user.setEmp(emp);
        user.setNickname(nickname);

        appUserService.AppUserLogin(user, session);
        return new ResponseResult<>(SUCCESS);
    }

    /**
     * 小程序获取手机号
     *
     * @param mag
     * @param iv
     * @param session
     * @return
     */
    @RequestMapping("getphone")
    public ResponseResult<User> getPhone(String mag, String iv,String openid,String img,String nickName, HttpSession session) {

        System.err.println("openid="+openid);
        String sessionKey = session.getAttribute("session_key").toString();
        String appOpenid = session.getAttribute("app_openid").toString();
        String token = session.getAttribute("token").toString();
        User data = appUserService.GetPhone(mag, iv, sessionKey, token, appOpenid,openid,img,nickName,session);
        session.setAttribute("uid", data.getId());
        session.setAttribute("phone", data.getPhone());

        data.setPhone(data.getPhone().replaceAll("[a-zA-Z]", ""));
        return new ResponseResult<User>(SUCCESS, data);
    }

    /**
     * 通过openid登录 缓存token 返回手机号 若无此vip则返回null 小程序禁止访问个人信息部分
     *
     * @param code
     * @param token
     * @param session
     * @return
     */
    @RequestMapping("log")
    public ResponseResult<User> Logging(String code, String token, HttpSession session) {
        System.err.println("code="+code);
        System.err.println("token="+token);
        System.err.println("sessionId="+session.getId());
        session.setAttribute("token", token);

        User data = appUserService.Log(code, token, session);
        System.err.println(data);
        System.err.println("session**"+session.getAttribute("phone"));
        return new ResponseResult<User>(SUCCESS, data);
    }

    @RequestMapping("data")
    public ResponseResult<User> GetData(HttpSession session) {
        String token = session.getAttribute("token").toString();
        String openid = session.getAttribute("app_openid").toString();
        User data = appUserService.GetUserData(openid, token);
        return new ResponseResult<User>(SUCCESS, data);
    }


}
